<?php

require_once 'session.php';
require_once 'shared-functions.php';

/**
 * Contains functions for using the masterpage
 *
 * @author Ryan Olson
 */

/**
 * Global Variables
 */
$STUDENT = 1;
$TEACHER = 2;
$PRINCIPLE = 3;
$DIRECTOR = 4;
$REGISTRAR = 5;
$LIBRARIAN = 6;
$ADMIN = 7;


/**
 * Masterpage function generates the header content and sets up the page
 * which in turn allows the user to enter into the content div.
 *
 * @Author Ryan Olson
 * @Version 1.0
 *
 * @param $subtitle subtitle of the page (Ex. Add new user, Login, etc)
 */
function masterpage($subtitle)
{
    print <<<HTML
	<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
	<html>
	<head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
	<link rel="stylesheet" type="text/css" href="master.css" />
	<style type="text/css">
		#signOutLink {color:white;}
	</style>
	<title>MLHTS - $subtitle</title>
	</head>
	<body>
		<div class="banner">
		    <div class="title">
                MLHTS
            </div>
            <div class="subtitle">
                $subtitle
            </div>
			<img src="images/banner2.jpg" width="100%" height="100%">
		</div>

		<div class="navbar">
			<table class="nav">
				<tr>
					<td width="75"><a href="index.php" class="nav">Home</a></td>
					<td width="75"><a href="profile.php" class="nav">Profile</a></td>
					<td width="100"><a href="contact.php" class="nav">Contact</a></td>


HTML;
                    //If logged in
                    if (IsValidSession()) {
                        echo "<td class='username'>";
                        echo "(Logged in as ";
                        echo GetUsername();
                        echo ', <a href="logout.php" id="signOutLink">Sign out</a>)';
                        echo "</td>";
                    }
    print <<<HTML
                </tr>
			</table>
		</div>
HTML;

    echo "<div class='contentPane'>";
    echo '<table width="100%">
                <tr>
                    <td valign="top" style="background-color: #EDEDED; width:20%;">';
                        sidebar();

    echo '
                    </td>
                    <td valign="top" style="padding: 16px;">';
}

/**
 * Function returns the username of the user currently logged in via session
 * @author Ryan Olson
 * @version 1.0
 */
function GetUsername()
{
    $link = connect_db();
    $userID = GetSessionUser();
    $query = "Select * FROM User WHERE UserId = '" . $userID . "';";
    $result = mysql_query($query, $link);
    $row = mysql_fetch_array($result);
    $username = $row[1];

    return $username;
}

/**
 * Function used to create the side navigation bar
 *
 * @author Ryan Olson
 * @version 1.0
 */
function sidebar()
{
    global $STUDENT, $TEACHER, $PRINCIPLE, $DIRECTOR, $REGISTRAR, $LIBRARIAN, $ADMIN;
	
    echo '<table class="sidetable" align="right">';

    if (IsValidSession()) {
        try
        {
            $privilege = GetCurrentUserAccessLevel();
        }
        catch (Exception $exception)
        {
            $_SESSION['error'] = $exception->getMessage();
            return false;
        }

        switch ($privilege)
        {
            /*
                   View Grades
                   View Fees
               */
            case $STUDENT:
                echo '<tr><td class="sidebarPrivName">Student Links</td></tr>';
                echo '<tr><td><a class="side" href="course-selector.php?page=view-grades&filter=student">View Grades</a></td></tr>';
                echo '<tr><td><a class="side" href="student-fees.php">View Fees</a></td></tr>';
                break;

            /*
                   Manage Assignments
               */
            case $TEACHER:
                echo '<tr><td class="sidebarPrivName">Teacher Links</td></tr>';
                echo '<tr><td><a class="side" href="course-selector.php?page=manage-assignments&filter=teacher">Manage Assignments</a></td></tr>';
                break;

            /*
                   Manage Programs
                   Manage Courses
                   Manage Semesters
                   Manage Student Grades
				   Manage Student Fees
				   Generate Reports
				   View Notifications
               */
            case $PRINCIPLE:
                echo '<tr><td class="sidebarPrivName">Principle Links</td></tr>';
				echo '<tr><td><a class="side" href="manage-programs.php">Manage Programs</a></td></tr>';
                echo '<tr><td><a class="side" href="manage-courses.php">Manage Courses</td></tr>';
                echo '<tr><td><a class="side" href="manage-semesters.php">Manage Semesters</a></td></tr>';
                echo '<tr><td><a class="side" href="course-selector.php?page=manage-assignments">Manage Student Grades</a></td></tr>';
                echo '<tr><td><a class="side" href="student-selector.php?page=apply-fee">Manage Student Fees</a></td></tr>';
                echo '<tr><td><a class="side" href="reports.php">Generate Reports</a></td></tr>';
                echo '<tr><td><a class="side" href="notifications.php">View Notifications</a></td></tr>';
                break;

            /*
                   Manage Programs
                   Manage Courses
                   Manage Semesters
                   Manage Users (Add new user included)
                   Modify Student Fees
                   Manage Student Grades
                   Manage Students
                   Register Students
                   Process Payments
               */
            case $ADMIN: //If admin ever differs from director permissions, it will need to be it's own case
            case $DIRECTOR:
                echo '<tr><td class="sidebarPrivName">Admin/Director Links</td></tr>';
				echo '<tr><td><a class="side" href="manage-programs.php">Manage Programs</a></td></tr>';
                echo '<tr><td><a class="side" href="manage-courses.php">Manage Courses</a></td></tr>';
                echo '<tr><td><a class="side" href="manage-semesters.php">Manage Semesters</a></td></tr>';
                echo '<tr><td><a class="side" href="user-selector.php?page=edit-profile">Manage Users</a></td></tr>';
                echo '<tr><td><a class="side" href="apply-fee.php">Manage Student Fees</a></td></tr>';
                echo '<tr><td><a class="side" href="course-selector.php?page=manage-assignments">Manage Student Grades</a></td></tr>';
                echo '<tr><td><a class="side" href="student-selector.php?page=edit-profile&select=Edit">Manage Students</a></td></tr>';
                echo '<tr><td><a class="side" href="register.php">Register Students</a></td></tr>';
                echo '<tr><td><a class="side" href="process-payment.php">Process Payments</a></td></tr>';
                echo '<tr><td><a class="side" href="reports.php">Generate Reports</a></td></tr>';
				echo '<tr><td><a class="side" href="view-log.php">View Change Log</a></td></tr>';
                echo '<tr><td><a class="side" href="backup.php">Manage System Backups</a></td></tr>';
                echo '<tr><td><a class="side" href="setup.php">Manage System Setup</a></td></tr>';
                echo '<tr><td><a class="side" href="notifications.php">View Notifications</a></td></tr>';
                break;

            /*
                   Add New Student
				   Register Students
				   Manage Student Grades
				   Manage Student Fees
				   Process Payment
				   Generate Reports
               */
            case $REGISTRAR:
                echo '<tr><td class="sidebarPrivName">Registrar Links</td></tr>';
                echo '<tr><td><a class="side" href="add-new-student.php">Add New Student</a></td></tr>';
                echo '<tr><td><a class="side" href="register.php">Register Students</a></td></tr>';
                echo '<tr><td><a class="side" href="course-selector.php?page=manage-assignments">Manage Student Grades</a></td></tr>';
                echo '<tr><td><a class="side" href="apply-fee.php">Manage Student Fees</a></td></tr>';
                echo '<tr><td><a class="side" href="process-payment.php">Process Payment</a></td></tr>';
                echo '<tr><td><a class="side" href="reports.php">Generate Reports</a></td></tr>';
                break;

            /*
                   Add Late Fees
               */
            case $LIBRARIAN:
                echo '<tr><td class="sidebarPrivName">Librarian Links</td></tr>';
                echo '<tr><td><a class="side" href="apply-fee.php">Add Late Fees</a></td></tr>';
                break;
        }
    }

    echo "</table>";
    //echo "</div>";
}

/**
 * Function used after masterpage() to close out the content div, body and html
 *
 * @author Ryan Olson
 * @version 1.0
 */
function endmasterpage()
{
    echo'       </td>
            </tr>
          </table>';
    echo '</div>'; //end content pane div

    echo '
    <div class="footer">
        <table width="100%">
            <tr>
                <td style="text-align:center; font-size: 60%;">
                    &copy; 2010-2011 Weber State University. All rights reserved.
                </td>
            </tr>
        </table>

        </span>
	</div>
	</body>
	</html>';
}

?>
